var echarts;
var sjk;
$(document).ready(function () {
    require.config({
        baseUrl: ctx + '/resource/plugins/echarts4',
        paths: {
            echarts: 'echarts',
            roma: 'theme/roma',
        }
    });
    require(
        [
            'echarts',
            'roma'
        ],
        function (ec) {
            ECharts = ec;
            queryDatabaseInfo();

            queryZtYbp();//数据库可用性

            qyeryJkdYbp();//数据库健康度

            database_hhs_top = ECharts.init(document.getElementById('database_hhs_top'), 'roma');//会话数量
            queryHhsTop();

            database_ssl_top = ECharts.init(document.getElementById('database_ssl_top'), 'roma');//锁数量
            querySslTop();

            database_qksjl_top = ECharts.init(document.getElementById('database_qksjl_top'), 'roma');//全库数据量
            queryQksjlTop();
        }
    );
});

/**数据库基本信息*/
function queryDatabaseInfo() {
    $.ajax({
        url: ctx + "/HostPage/queryTableInfo",
        type: "post",
        async: false,
        data: {CI_TYPE: CI_TYPE, DI_QU: DI_QU},
        success: function (data) {
            var map = JSON.parse(data);
            var list = map.list;
            var list1 = map.list1;
            var tablel = "";
            if (list1 == undefined || list == undefined || list == '' || list1 == '') {
                tablel += "<ul>" +
                    "<li class='first' style='width:33.3%'>" + "分类" + "</li>" +
                    " <li style='width:33.3%'>" + "资源总数" + "</li>" +
                    "<li class='last' style='width:33.3%'>" + "暂无数据" + "</li>" +
                    "</ul>" +
                    "<ul>" +
                    "<li class='first' style='width:33.3%'>" + "数量" + "</li>" +
                    " <li style='width:33.3%'>" + "暂无数据" + "</li>" +
                    "<li class='last' style='width:33.3%'>" + "暂无数据" + "</li>" +
                    "</ul>";
                $("#database_info").append(tablel);
            } else {
                sjk = list1[0].ZSL;
                tablel += "<ul>" +
                    "<li class='first' style='width:33.3%'>" + "分类" + "</li>" +
                    " <li style='width:33.3%'>" + "资源总数" + "</li>" +
                    "<li class='last' style='width:33.3%'>" + list[0].CLASSNAME + "</li>" +
                    "</ul>" +
                    "<ul>" +
                    "<li class='first' style='width:33.3%'>" + "数量" + "</li>" +
                    " <li style='width:33.3%'>" + list1[0].ZSL  + "</li>" +
                    "<li class='last' style='width:33.3%'>" + list[0].SL  + "</li>" +
                    "</ul>";
                $("#database_info").append(tablel);
            }
        }, error: function (textStatus) {
            console.log("error:" + textStatus.status);
        }
    });
}

/**数据库可用性*/
//PM-00-00-000-02
function queryZtYbp() {
    $.ajax({
        url: ctx + "/HostPage/queryYbp",
        type: "post",
        data: {CI_TYPE: CI_TYPE, KPI_CODE: "FM-00-03-001-01"},
        success: function (data) {
            var cpuState = JSON.parse(data);
            var list = cpuState.list;
            if (list == undefined) {
                var qq = "<div class='easy-pie-chart percentage' data-percent='' id='' data-color='#87B87F'style='height: 75px; width: 75px; line-height: 74px; color: rgb(135, 184, 127);'>" +
                    "<span class='percent' style='top:0px;left:40px;'>" + "暂无数据" + "</span>" +
                    "<canvas height='75' width='75'>" + "</canvas>" + "</div>" + "<h3 style='line-height:3;'>" + "可用性" + "</h3>";
                $("#database_zt_ybp").append(qq);
            } else {
                var qq = "<div class='easy-pie-chart percentage' data-percent='' id='sjkzt' data-color='#5eadf0'style='height: 75px; width: 75px; line-height: 74px; color: rgb(135, 184, 127);'>" +
                    "<span class='percent' style='top:0px;left:40px;'>" + parseInt(cpuState[0].SUCCESS_VALUE / cpuState[0].COUNT_VALUE * 100) + "%" + "</span>" +
                    "<canvas height='75' width='75'>" + "</canvas>" + "</div>" + "<h3 style='line-height:3;'>" + "可用性" + "</h3>";
                $("#database_zt_ybp").append(qq);
                $('#sjkzt').attr('data-percent', parseInt(cpuState[0].SUCCESS_VALUE / cpuState[0].COUNT_VALUE * 100));
            }
            $('.easy-pie-chart.percentage').each(function () {
                $(this).easyPieChart({
                    barColor: $(this).data('color'),
                    trackColor: '#EEEEEE',
                    scaleColor: false,
                    lineCap: 'butt',
                    lineWidth: 8,
                    animate: ace.vars['old_ie'] ? false : 1000,
                    size: 120
                }).css('color', $(this).data('color'));
            });
        }, error: function (textStatus) {
            console.log("error:" + textStatus.status);
        }
    });

}

//数据库健康度
function qyeryJkdYbp() {
    $.ajax({
        url: ctx + "/HostPage/queryJkd",
        type: "post",
        data: {CI_TYPE: CI_TYPE, DI_QU: DI_QU},
        success: function (data) {
            var cpuState = JSON.parse(data);
            if (sjk == undefined) {
                var qq = "<div class='easy-pie-chart percentage' data-percent='' id='' data-color='#87B87F'style='height: 75px; width: 75px; line-height: 74px; color: rgb(135, 184, 127);'>" +
                    "<span class='percent' style='top:0px;left:12px;'>" + "暂无数据" + "</span>" +
                    "<canvas height='75' width='75'>" + "</canvas>" + "</div>" + "<h3 style='line-height:3;'>" + "健康度" + "</h3>";
                $("#database_jkd").append(qq);
            } else {
                var qq = "<div class='easy-pie-chart percentage' data-percent='' id='sjkjkd' data-color='#87B87F'style='height: 75px; width: 75px; line-height: 74px; color: rgb(135, 184, 127);'>" +
                    "<span class='percent' style='top:0px;left:40px;'>" + parseInt((sjk - cpuState[0].EXT_UNIT_ID) / sjk * 100) + "%" + "</span>" +
                    "<canvas height='75' width='75'>" + "</canvas>" + "</div>" + "<h3 style='line-height:3;'>" + "健康度" + "</h3>";
                $("#database_jkd").append(qq);
                $('#sjkjkd').attr('data-percent', parseInt((sjk - cpuState[0].EXT_UNIT_ID) / sjk * 100));
            }
            $('.easy-pie-chart.percentage').each(function () {
                $(this).easyPieChart({
                    barColor: $(this).data('color'),
                    trackColor: '#EEEEEE',
                    scaleColor: false,
                    lineCap: 'butt',
                    lineWidth: 8,
                    animate: ace.vars['old_ie'] ? false : 1000,
                    size: 120
                }).css('color', $(this).data('color'));
            });
        }, error: function (textStatus) {
            console.log("error:" + textStatus.status);
        }
    });

}

//会话数量top榜
//PM-00-01-001-05
function queryHhsTop() {
    $.ajax({
        url: ctx + "/HostPage/queryTop",
        type: "post",
        data: {KPI_CODE: "KPI-oracle-all-36", DI_QU: DI_QU},
        success: function (data) {
            var map = JSON.parse(data);
            var list = map.list;
            var xdata = [];
            var ydata = [];
            var x_value = [];
            for (var i = 0; i < list.length; i++) {
                x_value = list[i].IP;
                xdata.push(x_value);
                ydata.push(list[i].AVG_VALUE);
            }
            if (x_value == undefined || x_value == null || x_value == '') {
                var table = "<h3 style='margin-top: 150px;padding-left: 700px;'>" + "暂无数据" + "</h3>";
                $("#database_hhs_top").html(table);
            } else {
                option = {
                    color: ['#3398DB'],
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                            type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                        }
                    },
                    legend: {
                        formatter: function (series) {
                            return series;
                        },
                        textStyle: {
                            color: '#AAAAAA'
                        }
                    },
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },
                    xAxis: [
                        {
                            type: 'category',
                            // data: [1, 12, 23, 3,],
                            data: xdata,
                            axisTick: {
                                alignWithLabel: true
                            },
                            axisLabel: {
                                interval: 0,
                                rotate: 45,
                                margin: 8
                            }
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value',
                            // max: 100,
                            splitLine: {
                                show: true,
                            },
                            nameTextStyle: {
                                color: '#000000',
                            },
                            axisLabel: {
                                formatter: '{value} ',
                                textStyle: {
                                    color: '#000000'
                                }
                            }
                        },
                    ],
                    series: [
                        {
                            name: '会话数量',
                            type: 'bar',
                            barWidth: '60%',
                            // color: '#3398DB',
                            itemStyle: {
                                normal: {
                                    color: new ECharts.graphic.LinearGradient(
                                        0, 0, 0, 1,
                                        [
                                            {offset: 0, color: '#acc7f6'},
                                            {offset: 0.5, color: '#5eadf0'},
                                            {offset: 1, color: '#3621f0'}
                                        ]
                                    )
                                },
                            },
                            data: ydata,
                        }
                    ]
                };
                database_hhs_top.setOption(option);
            }
        }, error: function (textStatus) {
            console.log("error:" + textStatus.status);
        }
    });
}

//锁数量top榜
function querySslTop() {
    $.ajax({
        url: ctx + "/HostPage/queryTop",
        type: "post",
        data: {KPI_CODE: "KPI-oracle-all-45", DI_QU: DI_QU},
        success: function (data) {
            var map = JSON.parse(data);
            var list = map.list;
            var xdata = [];
            var ydata = [];
            var x_value = [];
            for (var i = 0; i < list.length; i++) {
                x_value = list[i].IP;
                xdata.push(x_value);
                ydata.push(list[i].AVG_VALUE);
            }
            if (x_value == undefined || x_value == null || x_value == '') {
                var table = "<h3 style='margin-top: 150px;padding-left: 700px;'>" + "暂无数据" + "</h3>";
                $("#database_ssl_top").html(table);
            } else {
                option = {
                    color: ['#3398DB'],
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                            type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                        }
                    },
                    legend: {
                        formatter: function (series) {
                            return series;
                        },
                        textStyle: {
                            color: '#AAAAAA'
                        }
                    },
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },
                    xAxis: [
                        {
                            type: 'category',
                            // data: [1, 12, 23, 3,],
                            data: xdata,
                            axisTick: {
                                alignWithLabel: true
                            },
                            axisLabel: {
                                interval: 0,
                                rotate: 45,
                                margin: 8
                            }
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value',
                            // max: 100,
                            splitLine: {
                                show: true,
                            },
                            nameTextStyle: {
                                color: '#000000',
                            },
                            axisLabel: {
                                formatter: '{value} ',
                                textStyle: {
                                    color: '#000000'
                                }
                            }
                        },
                    ],
                    series: [
                        {
                            name: '锁数量',
                            type: 'bar',
                            barWidth: '60%',
                            // color: '#3398DB',
                            itemStyle: {
                                normal: {
                                    color: new ECharts.graphic.LinearGradient(
                                        0, 0, 0, 1,
                                        [
                                            {offset: 0, color: '#acc7f6'},
                                            {offset: 0.5, color: '#5eadf0'},
                                            {offset: 1, color: '#3621f0'}
                                        ]
                                    )
                                },
                            },
                            data: ydata,
                        }
                    ]
                };
                database_ssl_top.setOption(option);
            }
        }, error: function (textStatus) {
            console.log("error:" + textStatus.status);
        }
    });
}

//全库数据量top榜
function queryQksjlTop() {
    $.ajax({
        url: ctx + "/HostPage/queryTop",
        type: "post",
        data: {KPI_CODE: "KPI-oracle-all-34", DI_QU: DI_QU},
        success: function (data) {
            var map = JSON.parse(data);
            var list = map.list;
            var xdata = [];
            var ydata = [];
            var x_value = [];
            for (var i = 0; i < list.length; i++) {
                x_value = list[i].IP;
                xdata.push(x_value);
                ydata.push(list[i].AVG_VALUE);
            }
            if (x_value == undefined || x_value == null || x_value == '') {
                var table = "<h3 style='margin-top: 150px;padding-left: 700px;'>" + "暂无数据" + "</h3>";
                $("#database_qksjl_top").html(table);
            } else {
                option = {
                    color: ['#3398DB'],
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                            type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                        }
                    },
                    legend: {
                        formatter: function (series) {
                            return series;
                        },
                        textStyle: {
                            color: '#AAAAAA'
                        }
                    },
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },
                    xAxis: [
                        {
                            type: 'category',
                            // data: [1, 12, 23, 3,],
                            data: xdata,
                            axisTick: {
                                alignWithLabel: true
                            },
                            axisLabel: {
                                interval: 0,
                                rotate: 45,
                                margin: 8
                            }
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value',
                            // max: 100,
                            splitLine: {
                                show: true,
                            },
                            nameTextStyle: {
                                color: '#000000',
                            },
                            axisLabel: {
                                formatter: '{value} ',
                                textStyle: {
                                    color: '#000000'
                                }
                            }
                        },
                    ],
                    series: [
                        {
                            name: '全库数据量',
                            type: 'bar',
                            barWidth: '60%',
                            // color: '#3398DB',
                            itemStyle: {
                                normal: {
                                    color: new ECharts.graphic.LinearGradient(
                                        0, 0, 0, 1,
                                        [
                                            {offset: 0, color: '#acc7f6'},
                                            {offset: 0.5, color: '#5eadf0'},
                                            {offset: 1, color: '#3621f0'}
                                        ]
                                    )
                                },
                            },
                            data: ydata
                        }
                    ]
                };
                database_qksjl_top.setOption(option);
            }
        }, error: function (textStatus) {
            console.log("error:" + textStatus.status);
        }
    });
}
